package cn.dglydrpy.study.j2ee.servlet.day12;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 控制器 
 * 1.根据请求路径来调用合适的模型来处理请求
 * 2.依据模型返回的处理结果,调用合适的视图返回给视图
 */
public class BmiServlet3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 依据升高体重计算bmi指数对应的状态
		 */
		
		//获取请求资源路径:
		request.setCharacterEncoding("utf-8");
		
		String url = request.getRequestURI();
		String path = url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
		System.out.println("path:"+path);
		
		if("/toBmi3".equals(path)){//转发到jsp
			request.getRequestDispatcher("/WEB-INF/bmi3.jsp").forward(request, response);
		}
		
		if ("/js".equals(path)) {
			double height = Double.parseDouble(request.getParameter("height"));
			double weight = Double.parseDouble(request.getParameter("weight"));
			
			BmService service = new BmService();
			String msg = service.bmi(height, weight);
			
			request.setAttribute("msg", "您的身体状况是: "+msg);
			request.getRequestDispatcher("/WEB-INF/bmi3.jsp").forward(request, response);
		}	
	}

}
